package com.hudongwx.food.tiaozhuan_fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.food.R;
import com.hudongwx.food.activity.XiangqingActivity;
import com.hudongwx.food.utils.MyJsonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_hao_jiu)
public class HaoJiuFragment extends Fragment {
private View view;
    private MyHaoYouAdapter myHaoYouAdapter;
    @ViewInject(R.id.haoyou_recyclerView)
    private RecyclerView recyclerView;
    private JSONArray jsonArray;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = x.view().inject(this, inflater, container);
            myHaoYouAdapter = new MyHaoYouAdapter(jsonArray);
            LinearLayoutManager manager = new LinearLayoutManager(getActivity());
            recyclerView.setLayoutManager(manager);
            recyclerView.setAdapter(myHaoYouAdapter);
            getHaoYou();
        }
        return view;
    }
    public void getHaoYou() {
        RequestParams params = new RequestParams("http://api.shiandianping.com/api/sourceAndroidDianPing/v3.1.0/Product?brand=GWYS");
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myHaoYouAdapter.getItem(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    class MyHaoYouAdapter extends RecyclerView.Adapter<MyHaoYouViewHolder> {
        JSONArray array;

        public MyHaoYouAdapter(JSONArray array) {
            this.array = array;
        }

        public void getItem(JSONArray array) {
            this.array = array;
            notifyDataSetChanged();
        }

        @Override
        public MyHaoYouViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(getActivity()).inflate(R.layout.haoyou_list, null);
            return new MyHaoYouViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(MyHaoYouViewHolder holder, final int position) {
            if (null==array){
                recyclerView.setVisibility(View.GONE);
            }else {
                recyclerView.setVisibility(View.VISIBLE);
                try {
                    holder.textView1.setText(array.getJSONObject(position).getString("ProductName"));
                    holder.textView2.setText(array.getJSONObject(position).getString("ZMShortDescribes"));
                    x.image().bind(holder.imageView1,array.getJSONObject(position).getString("HomeImgUrl"));
                    holder.btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent=new Intent(getContext(), XiangqingActivity.class);
                            try {
                                String string=array.getJSONObject(position).getString("ProductID");
                                intent.putExtra("type",string);
                                getActivity().startActivity(intent);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }

        @Override
        public int getItemCount() {
            if (null == array) return 0;
            return array.length();
        }
    }

    class MyHaoYouViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView1,imageView2;
        private TextView textView1,textView2;
        private Button btn;
        public MyHaoYouViewHolder(View itemView) {
            super(itemView);
            imageView1 = (ImageView) itemView.findViewById(R.id.haoyou_img1);
//            imageView2 = (ImageView) itemView.findViewById(R.id.haoyou_img2);
            textView1 = (TextView) itemView.findViewById(R.id.haoyou_tv1);
            textView2 = (TextView) itemView.findViewById(R.id.haoyou_tv2);
            btn = (Button) itemView.findViewById(R.id.haoyou_btn);
        }
    }
}
